#!/bin/sh /usr/share/dpatch/dpatch-run
## 01_previous_upstream_patches_transitions.dpatch by Kartik Mistry <kartik.mistry@gmail.com>
## See changelog for patch contributors
## DP: Patch for previous applied patches transition to dpatch

@DPATCH@

--- xmountains-2.7.orig/xmountains.c
+++ xmountains-2.7/xmountains.c
@@ -1,6 +1,9 @@
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
+#include <sys/types.h>
+#include <unistd.h>
 #include "crinkle.h"
 #include "paint.h"
 #include "patchlevel.h"
@@ -15,6 +18,8 @@
 char *display;
 extern char *geom;
 
+extern int swosh;
+
 /* {{{ my version on getopt*/
 int optind=1;
 char *optarg;
@@ -77,7 +82,7 @@
 
 double atof();
 #ifdef ANSI
-void init_graphics (int, unsigned long, int, int *, int *, int, Gun *, Gun *, Gun *);
+void init_graphics (int, Window, int,int, Graph *, Gun *, Gun *, Gun *);
 void clear_col( int );
 void finish_graphics();
 void plot_pixel (int, int, unsigned char);
@@ -130,14 +135,19 @@
   return id;
 }
 
-main (argc,argv)
+void init_parameters();
+void print_algorithm();
+void seed_uni(int ijkl);
+void plot_column(Graph *g);
+
+int main (argc,argv)
 int argc;
 char **argv;
 {
-  int i,p;
+  int i;
   int e_events=FALSE;
   int request_clear=FALSE;
-  int smooth;
+  int smooth = 0;
   int snooze=10;
   int root= 0;
   unsigned long window_id = 0;
@@ -149,6 +159,7 @@
   char *mesg[2];
   Gun *clut[3];
   FILE *pidfile;
+  swosh = 0;          /* default is ! -w */
 
   init_parameters();
   /* {{{ handle command line flags*/
@@ -157,9 +168,13 @@
 
   mesg[0]="false";
   mesg[1]="true";
-  while((c = my_getopt(argc,argv,"bxmqMEHl:r:f:t:I:A:S:T:W:C:a:p:B:n:R:g:d:c:e:v:Z:s:X:Y:P:F:G:"))!= -1)
+  while((c = my_getopt(argc,argv,"wbxmqMEHl:r:f:t:I:A:S:T:W:C:a:p:B:n:R:g:d:c:e:v:Z:s:X:Y:P:F:G:"))!= -1)
   {
     switch(c){
+      case 'w':
+        swosh = TRUE;               /* update window only when pixmap is complete */
+        break;
+
       case 'b':
         root = 1- root;
         break;                      /* run on root window */
@@ -354,7 +369,8 @@
   if( errflg )
   {
     fprintf(stderr,"%s: version %d.%d\n",argv[0],VERSION,PATCHLEVEL);
-    fprintf(stderr,"usage: %s -[bqgdPEmMrBZIASFTCapcevfRltxsXYH]\n",argv[0]);
+    fprintf(stderr,"usage: %s -[wbqgdPEmMrBZIASFTCapcevfRltxsXYH]\n",argv[0]);
+    fprintf(stderr," -w       [%s] update window with complete images only\n",mesg[swosh]);
     fprintf(stderr," -b       [%s] use root window \n",mesg[root]);
     fprintf(stderr," -q       [%s] reset root window on exit\n",mesg[request_clear]);
     fprintf(stderr," -g string     window geometry\n");
@@ -405,7 +421,8 @@
     }
   }
   set_clut(g.n_col,clut[0], clut[1], clut[2]);
-  init_graphics(root, window_id, (! e_events),request_clear,&g.graph_width,&g.graph_height,g.n_col,clut[0],clut[1],clut[2]);
+ init_graphics(root,window_id, !e_events, request_clear, &g, clut[0], clut[1], clut[2]);
+
   for(i=0;i<3;i++)
   {
     free(clut[i]);
@@ -415,22 +432,22 @@
   seed_uni(seed);
 
   init_artist_variables();
-  if( -1 == (int) signal(SIGINT, finish_prog ))
+  if( SIG_ERR == signal(SIGINT, finish_prog ))
   {
     perror(argv[0]);
     exit(1);
   }
-  if( -1 == (int) signal(SIGTERM, finish_prog ))
+  if( SIG_ERR == signal(SIGTERM, finish_prog ))
   {
     perror(argv[0]);
     exit(1);
   }
-  if( -1 == (int) signal(SIGHUP, finish_prog ))
+  if( SIG_ERR == signal(SIGHUP, finish_prog ))
   {
     perror(argv[0]);
     exit(1);
   }
-  if( -1 == (int) signal(SIGQUIT, finish_prog ))
+  if( SIG_ERR == signal(SIGQUIT, finish_prog ))
   {
     perror(argv[0]);
     exit(1);
--- xmountains-2.7.orig/X_graphics.c
+++ xmountains-2.7/X_graphics.c
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include<X11/Xlib.h>
 #include<X11/Xutil.h>
 #include<X11/Xatom.h>
@@ -30,6 +31,8 @@
   int do_clear=FALSE;
   int pixmap_installed=FALSE;
 
+  int swosh=FALSE;
+
   /* plot history */
   int plot_x, plot_y1, plot_y2;
   unsigned long plot_col;
@@ -41,6 +44,7 @@
   unsigned int depth=0;
   GC gc;
   Pixmap pix;
+  Pixmap bg_pix;
   Colormap map, defaultmap;
   XColor *table=NULL;
 void zap_events();
@@ -50,6 +54,7 @@
 1024,
 768,
 0,
+0,
 0.3,
 1.0,
 0.3,
@@ -72,7 +77,7 @@
 0
 };
 
-
+void finish_artist();
 
 /*{{{void zap_events(int snooze)*/
 void zap_events(snooze)
@@ -80,7 +85,7 @@
 {
   XEvent event;
   XExposeEvent *expose = (XExposeEvent *)&event;
-  int exw, exh, i;
+  int exw, exh;
 
   while( XPending(dpy) ){
     XNextEvent(dpy, &event);
@@ -111,11 +116,20 @@
             }else{
               exh = expose->height;
             }
-            XCopyArea(dpy,pix,win,gc,expose->x,expose->y,
-                          exw,exh,
-                          expose->x,expose->y);
-	  }
-	  break;
+            if(g.repeat < 0)
+            {
+              XCopyArea(dpy, pix, win, gc, expose->x - g.repeat, expose->y,
+                            exw, exh,
+                            expose->x,expose->y);
+            }
+            else
+            {
+              XCopyArea(dpy,pix,win,gc,expose->x,expose->y,
+                            exw,exh,
+                            expose->x,expose->y);
+            }
+          }
+          break;
         default:
             fprintf(stderr,"xmountains: unrecognized event %d\n",event.type);
             /* XCloseDisplay(dpy);
@@ -170,7 +184,7 @@
   {
     XFreeColormap(dpy, map );
   }
-  if( count = XPending(dpy) )
+  if((count = XPending(dpy)))
   {
     fprintf(stderr,"WARNING: %d events still pending\n",count);
   }
@@ -207,22 +221,22 @@
 /*}}}*/
 
 /*{{{void init_graphics( ... )*/
-void init_graphics( want_use_root, use_window, use_background, want_clear, s_graph_width,s_graph_height,ncol,red,green,blue )
+void init_graphics( want_use_root, use_window, use_background, want_clear,gptr,red,green,blue )
+
 int want_use_root;    /* display on the root window */
 Window use_window;    /* display on external window */
 int  use_background;  /* install the pixmap as the background-pixmap */
 int want_clear;
-int *s_graph_width;
-int *s_graph_height;
-int ncol;
+Graph *gptr;
+
 Gun *red;
 Gun *green;
 Gun *blue;
 {
 /*{{{defs*/
   Visual *vis;
-  int mask;
-  int count;
+
+  
   int x=0;
   int y=0;
   int gbits=0;
@@ -246,8 +260,8 @@
   do_clear = want_clear;
   use_root = want_use_root;
   pixmap_installed = use_background;
-  graph_width = *s_graph_width;
-  graph_height = *s_graph_height;
+  graph_width = gptr->graph_width;
+  graph_height = gptr->graph_height;
 /*{{{open display*/
 
   dpy = XOpenDisplay(display);
@@ -268,13 +282,13 @@
   depth = DefaultDepth(dpy,screen);
 /*}}}*/
 /*{{{set colormap*/
-  table=(XColor *)malloc(ncol * sizeof(XColor));
+  table = (XColor *)malloc(gptr->n_col * sizeof(XColor));
   if( NULL == table )
   {
     fprintf(stderr,"malloc failed for colour table\n");
     exit(1);
   }
-  for(i=0; i<ncol ; i++)
+  for(i = 0; i < gptr->n_col; i++)
   {
     table[i].red   = red[i];
     table[i].green = green[i];
@@ -378,10 +392,31 @@
 /*    graph_width = DisplayWidth(dpy,screen); */
 /*    graph_height = DisplayHeight(dpy,screen); */
   }
-  pix = XCreatePixmap(dpy,win,graph_width,graph_height,depth);
+
+  if(swosh)
+  {
+    if(gptr->repeat < 0)
+    {
+      gptr->pixmap_width = graph_width - gptr->repeat;
+      bg_pix = XCreatePixmap(dpy, win, graph_width, graph_height, depth);
+    }
+    else
+    {
+      if(gptr->repeat == 0)
+      {
+        gptr->repeat = graph_width;
+      }
+      gptr->pixmap_width = graph_width + gptr->repeat;
+    }
+  }
+  else
+  {
+    gptr->pixmap_width = graph_width;
+  }
+  pix = XCreatePixmap(dpy, win, gptr->pixmap_width, graph_height, depth);
 
 /*}}}*/
-  blank_region(0,0,graph_width,graph_height); 
+  blank_region(0, 0, gptr->pixmap_width, graph_height); 
 
   if( use_background )
   {
@@ -394,8 +429,8 @@
   XClearWindow(dpy,win);
   zap_events(0);
   
-  *s_graph_width = graph_width;
-  *s_graph_height = graph_height;
+  gptr->graph_width = graph_width;
+  gptr->graph_height = graph_height;
 }
 /*}}}*/
 
@@ -411,21 +446,21 @@
     reverse=TRUE;
   }
   /* scroll the pixmap */
-  if( dist > graph_width )
+  if( dist > g.pixmap_width )
   {
-    dist = graph_width;
+    dist = g.pixmap_width;
   }
   if( reverse )
   {
     /* copy the data */
-    XCopyArea(dpy,pix,pix,gc,0,0,graph_width-dist,graph_height,dist,0);
+    XCopyArea(dpy, pix, pix, gc, 0, 0, g.pixmap_width - dist, graph_height, dist, 0);
     /* blank new region */
     blank_region(0,0,dist,graph_height);
   }else{
     /* copy the data */
-    XCopyArea(dpy,pix,pix,gc,dist,0,graph_width-dist,graph_height,0,0);
+    XCopyArea(dpy, pix, pix, gc, dist, 0, g.pixmap_width - dist, graph_height, 0, 0);
     /* blank new region */
-    blank_region(graph_width-dist,0,dist,graph_height);
+    blank_region(g.pixmap_width - dist, 0, dist, graph_height);
   }
   /* update the window to match */
   if( pixmap_installed )
@@ -436,10 +471,26 @@
      * so in principle we should re-install.
      */
     XSetWindowBackgroundPixmap(dpy,win,None);
-    XSetWindowBackgroundPixmap(dpy,win,pix);
+
+    if(swosh && reverse)
+    {
+      XCopyArea(dpy, pix, bg_pix, gc, dist, 0, graph_width, graph_height, 0, 0);
+      XSetWindowBackgroundPixmap(dpy, win, bg_pix);
+    }
+    else
+    {
+      XSetWindowBackgroundPixmap(dpy,win,pix);
+    }
     XClearWindow(dpy,win);
   }else{
-    XCopyArea(dpy,pix,win,gc,0,0,graph_width,graph_height,0,0);
+    if(reverse)
+    {
+      XCopyArea(dpy, pix, win, gc, dist, 0, graph_width, graph_height, 0, 0);
+    }
+    else
+    {
+      XCopyArea(dpy, pix, win, gc, 0, 0, graph_width, graph_height, 0, 0);
+    }
   }
 
 }
@@ -511,7 +562,10 @@
 {
   /* flush outstanding plots */
   plot_pixel(-1,0,0);
-  XCopyArea(dpy,pix,win,gc,x,y,w,h,x,y);
+  if(!swosh)
+  {
+    XCopyArea(dpy,pix,win,gc,x,y,w,h,x,y);
+  }
 }
 /*}}}*/
 
--- xmountains-2.7.orig/vroot.h
+++ xmountains-2.7/vroot.h
@@ -76,6 +76,8 @@
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
 
+#include <stdlib.h>
+ 
 static Window
 #ifdef __STDC__ /* ANSIfication added by jwz, to avoid superfluous warnings. */
 VirtualRootWindowOfScreen(Screen *screen)
--- xmountains-2.7.orig/artist.c
+++ xmountains-2.7/artist.c
@@ -3,6 +3,7 @@
  */
 #include <math.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include "paint.h"
 #include "crinkle.h"
 
@@ -19,6 +20,8 @@
 extern Parm fold_param;
 extern Graph g;
 
+extern int swosh;
+
 Fold *top;
 
 
@@ -266,7 +269,7 @@
   if( g.repeat >= 0 ){
     g.pos=0;
   }else{
-    g.pos=g.graph_width-1;
+    g.pos = g.pixmap_width - 1;
   }	
 }
 /* }}} */
@@ -424,7 +427,7 @@
 Height *b;
 Height *shadow;
 {
-  int i, j, coord, last;
+  int i, coord, last;
   Col *res, col;
 
   /* this routine returns a perspective view of the surface */
@@ -816,6 +819,11 @@
   return(res);
 }
 
+void blank_region(int lx, int ly, int ux, int uy);
+void flush_region(int x, int y, int w, int h);
+void scroll_screen(int dist);
+void plot_pixel(int x, int y, Gun value);
+
 /* }}} */
 /* {{{  void plot_column(g)*/
 void plot_column(g)
@@ -837,7 +845,7 @@
       flush_region(0,0,g->graph_width,g->graph_height);
     }
   }
-  if( g->scroll ){
+  if( g->scroll && !swosh ){
     scroll_screen(g->scroll);
   }
 
@@ -876,10 +884,10 @@
   /* now update pos ready for next time */
   if( g->repeat >=0 ){
     g->pos++;
-    if(g->pos >= g->graph_width)
+    if(g->pos >= g->pixmap_width)
     {
       g->pos -=  g->repeat;
-      if( g->pos < 0 || g->pos > g->graph_width-1 )
+      if( g->pos < 0 || g->pos > g->pixmap_width-1 )
       {
         g->pos=0; 
       }else{
@@ -890,14 +898,18 @@
     g->pos--;
     if( g->pos < 0 ){
       g->pos -=   g->repeat;
-      if( g->pos < 0 || g->pos > (g->graph_width-1) ){
-	g->pos=g->graph_width-1;
+      if( g->pos < 0 || g->pos > (g->pixmap_width-1) ){
+	g->pos=g->pixmap_width-1;
       }else{
 	g->scroll = g->repeat;
       }
     }
   }
 
+  if(g->scroll && swosh)
+  {
+    scroll_screen(g->scroll);
+  }
 }
 /* }}} */
 
--- xmountains-2.7.orig/paint.h
+++ xmountains-2.7/paint.h
@@ -12,6 +12,8 @@
   int graph_height;     /* height of display */
   int graph_width ;     /* width of display */
 
+  int pixmap_width;     /* width of pixmap buffer */
+
   int width;            /* width of terrain strip */
 
   float ambient;        /* level of ambient light */
--- xmountains-2.7.orig/random.c
+++ xmountains-2.7/random.c
@@ -22,6 +22,7 @@
  * #include <sys/time.h>
  */
 #include <stdio.h>
+#include <stdlib.h>
 #include <math.h>
 typedef struct
 {
